package cn.edu.dutir.lucene;

import java.io.File;
import java.io.IOException;
import java.util.Date;

import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.index.IndexWriter;

public class LuceneIndexOptimizer {

	public static void main(String args[]) {
		String usage = "java cn.edu.dutir.unit.lucene.LuceneIndexBuilder <root_directory>";
		if (args.length == 0) {
			System.err.println("Usage: " + usage);
			System.exit(1);
		}
		File indexDir = new File(args[0]);
		if (!indexDir.exists()) {
			System.out.println(indexDir + "do not exist");
			System.exit(1);
		}
		Date start = new Date();
		try {
			IndexWriter writer = new IndexWriter(indexDir,
					new StandardAnalyzer(), false);
			System.out.println("Optimizing...");
			writer.optimize();
			writer.close();

			System.out.println(writer.docCount() + " documents");
			Date end = new Date();
			System.out.println(end.getTime() - start.getTime()
					+ " total milliseconds");

		} catch (IOException e) {
			System.out.println(" caught a " + e.getClass()
					+ "\n with message: " + e.getMessage());
		}
	}
}
